package com.luhuiguo.cms.server.service.restriction;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;

public class ArticleFilter implements Serializable {

    private final static long serialVersionUID = -3753517738068538028L;

    public final static String ID = "id";
    public final static String CHANNEL_ID = "channelId";
    public final static String CHANNEL_IDS = "channelIds";
    public final static String RANK = "rank";
    public final static String GRADE = "grade";
    public final static String STATUS = "status";

    private Long id;

    private Long channelId;

    private String channelIds;

    private Integer rank;

    private Integer grade;

    private Integer status;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getChannelId() {
        return channelId;
    }

    public void setChannelId(Long channelId) {
        this.channelId = channelId;
    }

    public String getChannelIds() {
        return channelIds;
    }

    public void setChannelIds(String channelIds) {
        this.channelIds = channelIds;
    }

    public Integer getRank() {
        return rank;
    }

    public void setRank(Integer rank) {
        this.rank = rank;
    }

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ArticleFilter [channelId=").append(channelId).append(", id=").append(id).append(", rank=")
                .append(rank).append(", grade=").append(grade).append(", status=").append(status).append("]");
        return builder.toString();
    }

    public String getWhere() {
        List<String> restrictions = new ArrayList<String>();
        if (null != id) {
            restrictions.add(ID + "=:" + ID);

        }
        if (null != channelId) {
            restrictions.add(CHANNEL_ID + "=:" + CHANNEL_ID);
        }
        if (null != channelIds) {
            restrictions.add(CHANNEL_ID + " in (:" + CHANNEL_IDS + ")");
        }

        if (null != rank) {
            restrictions.add(RANK + "=:" + RANK);
        }
        if (null != grade) {
            restrictions.add(GRADE + "=:" + GRADE);
        }
        if (null != status) {
            restrictions.add(STATUS + "=:" + STATUS);
        }
        return StringUtils.join(restrictions, " AND ");
    }

    public String toQueryString() {
        StringBuffer buf = new StringBuffer("SELECT FROM Article");
        String where = getWhere();
        if (StringUtils.isNotBlank(where)) {
            buf.append(" WHERE ").append(where);
        }
        buf.append(" ORDER BY lastModified desc");
        return buf.toString();
    }

    public String toCountString() {

        StringBuffer buf = new StringBuffer("SELECT COUNT() FROM Article");
        String where = getWhere();
        if (StringUtils.isNotBlank(where)) {
            buf.append(" WHERE ").append(where);
        }
        return buf.toString();
    }

    public void setParameters(Query q) {
        if (null != id) {
            q.setParameter(ID, id);
        }
        if (null != channelId) {
            q.setParameter(CHANNEL_ID, channelId);
        }
        if (null != channelIds) {
            q.setParameter(CHANNEL_IDS, channelIds);
        }
        if (null != rank) {
            q.setParameter(RANK, rank);
        }
        if (null != grade) {
            q.setParameter(GRADE, grade);
        }
        if (null != status) {
            q.setParameter(STATUS, status);
        }
    }

}
